package cn.iocoder.yudao.module.handguard.controller.admin.wealthrecords;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.handguard.controller.admin.wealthrecords.vo.SettingRateVO;
import cn.iocoder.yudao.module.handguard.controller.app.wealthrecord.vo.*;
import cn.iocoder.yudao.module.handguard.convert.wealthrecord.WealthRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.wealthrecord.WealthRecordDO;
import cn.iocoder.yudao.module.handguard.service.wealthrecord.WealthRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Tag(name = "小程序 - 用户-财富值明细记录")
@RestController
@RequestMapping("/hutai/wealth-record")
@Validated
public class WealthRecordsController {

    @Resource
    private WealthRecordService wealthRecordService;



    @PutMapping("/updateMyWealth")
    @Operation(summary = "更新用户-扣除-财富值明细记录")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:update')")
    public CommonResult<String> updateWealthRecord(@Valid @RequestBody WealthUpdateReqVO updateReqVO) {
        String msg = wealthRecordService.updateMyWealth(updateReqVO);
        return success(msg);
    }

    @GetMapping("/getRateValue")
    @Operation(summary = "获得用户/推荐人返利值")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:query')")
    public CommonResult<SettingRateVO> getRateValue(String key) {
        SettingRateVO rateVO = wealthRecordService.getRateValue(key);
        return success(rateVO);
    }


    @PutMapping("/updateRate")
    @Operation(summary = "更新用户/推荐人返利值")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:update')")
    public CommonResult<String> updateRate(@Valid @RequestBody SettingRateVO rateVO) {
        String msg = wealthRecordService.updateRate(rateVO);
        return success(msg);
    }


    @GetMapping("/get")
    @Operation(summary = "获得用户-财富值明细记录列表")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:query')")
    public CommonResult<List<WealthRecordRespVO>> getWealthRecordListByUserId(@RequestParam("userId") Long userId) {
        List<WealthRecordDO> list = wealthRecordService.getWealthRecordList(userId);
        return success(WealthRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getWealth")
    @Operation(summary = "获得用户-财富值")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:query')")
    public CommonResult<WealthRespVO> getWealthByUserId(@RequestParam("userId") Long userId) {
        WealthRespVO wealth = wealthRecordService.getWealthByUserId(userId);
        return success(wealth);
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户-财富值明细记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:query')")
    public CommonResult<PageResult<UserWealthRecordRespVO>> getWealthRecordPage(@Valid WealthRecordPageReqsVO pageVO) {
        PageResult<UserWealthRecordRespVO> pageResult = wealthRecordService.getUserWealthRecordPage(pageVO);
        return success(pageResult);
    }


//    @GetMapping("/export-excel")
//    @Operation(summary = "导出用户-财富值明细记录 Excel")
//    @PreAuthorize("@ss.hasPermission('hutai:wealth-record:export')")
//    @OperateLog(type = EXPORT)
//    public void exportWealthRecordExcel(@Valid WealthRecordExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<WealthRecordDO> list = wealthRecordService.getWealthRecordList(exportReqVO);
//        // 导出 Excel
//        List<WealthRecordExcelVO> datas = WealthRecordConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "用户-财富值明细记录.xls", "数据", WealthRecordExcelVO.class, datas);
//    }

}
